<template>
  <div class="subjects-container">
    <!-- 卡片区 -->
    <el-card>
      <el-row type="flex" justify="space-between">
        <el-col :span="18">
          <el-form :inline="true" ref="clearRormRef" :model="requestParameters">
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
                size="small"
                v-model.trim="requestParameters.subjectName"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="onclear">清除</el-button>
              <el-button size="small" type="primary" @click="getList"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="6">
          <el-button
            class="subjects-btn"
            type="success"
            size="small"
            @click="addSujectsList"
          >
            <i class="el-icon-edit"></i>
            新增学科
          </el-button>
        </el-col>
      </el-row>

      <!-- 提示的文案区 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- 表格区 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创造者"> </el-table-column>
        <el-table-column label="创建时间 " width="160px">
          <template slot-scope="{ row }">
            {{ row.addDate | parseTimeByString('{y}-{m}-{d} {h}:{i}:{s}') }}
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay " label="前台是否显示">
          <template slot-scope="{ row }">
            {{ row.isFrontDisplay === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="{ row }">
            <el-link
              :underline="false"
              type="primary"
              size="mini"
              @click="toDirectory(row.id, row.subjectName)"
              >学科分类</el-link
            >

            <el-link
              :underline="false"
              type="primary"
              size="mini"
              @click="toTags(row.id, row.subjectName)"
              >学科标签</el-link
            >

            <el-link
              :underline="false"
              type="primary"
              size="mini"
              @click="updateSub(row.id)"
              >修改</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              @click="deleteSub(row.id)"
              size="mini"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <PageTool
          :paginationPage="requestParameters.page"
          :paginationPagesize="requestParameters.pagesize"
          :total="total"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        ></PageTool>
      </div>
    </el-card>

    <!-- 新增修改弹窗 -->
    <subjects-add
      ref="updateRef"
      :subjectVisible.sync="subjectVisible"
      :updataId.sync="updataId"
      @subjectsAdd="subjectsAdd"
    />
  </div>
</template>
<script>
import { addSubjectsList, deleteSubject } from '@/api/hmmm/subjects'
import PageTool from '@/module-manage/components/page-tool'
import SubjectsAdd from '../components/subjects-add'

export default {
  name: '',
  components: {
    PageTool,
    SubjectsAdd
  },
  props: {},
  data() {
    return {
      tableData: [],
      total: 0,
      requestParameters: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      subjectVisible: false,

      updataId: null
    }
  },
  computed: {},
  watch: {},
  methods: {
    subjectsAdd() {
      this.getList()
    },

    async getList() {
      try {
        const res = await addSubjectsList(this.requestParameters)
        // console.log(res)
        this.tableData = res.data.items
        this.total = res.data.counts
      } catch (error) {
        this.$message.error('获取数据失败')
      }
    },

    // 每页显示条数
    handleSizeChange(newsize) {
      this.requestParameters.pagesize = newsize
      if (this.requestParameters.page === 1) {
        this.getList(this.requestParameters)
      }
    },
    // 当前页码
    handleCurrentChange(newpage) {
      this.requestParameters.page = newpage
      this.getList()
    },

    onclear() {
      this.$refs.clearRormRef.resetFields()
      this.getList()
    },

    addSujectsList() {
      this.subjectVisible = true
    },

    // 学科修改
    async updateSub(id) {
      this.updataId = id
      this.subjectVisible = true
      await this.$refs.updateRef.updateSubs(id)
    },
    // 学科删除
    async deleteSub(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除学科, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      try {
        await deleteSubject(id)
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        this.$message.info('您取消了删除')
      }
    },

    toDirectory(id, subjectName) {
      this.$router.push({
        path: '/subjects/directorys',
        name: 'subjects-directorys',
        params: {
          id,
          subjectName
        }
      })
    },

    toTags(id, subjectName) {
      this.$router.push({
        path: '/subjects/tags',
        name: 'subjects-tags',
        params: {
          id,
          subjectName
        }
      })
    }
  },
  created() {},
  mounted() {
    this.getList()
  }
}
</script>
<style scoped lang="scss">
.subjects-container {
  padding: 0 10px;
  margin: 10px 0;
  .subjects-btn {
    float: right;
  }
  .el-table {
    margin-top: 15px;
  }
  .pagination {
    margin-top: 10px;
    text-align: right;
  }
  .el-link {
    margin: 4px;
  }
}
</style>
